<template>
  <div>
    <div>
      <el-input
          size="small" v-model="jl.name" placeholder="请输入职称名称" prefix-icon="el-icon-plus" style="width: 300px;"/>
      <el-select size="small" v-model="jl.titlelevel" placeholder="职称等级" style="margin-left: 6px;margin-right: 6px;">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="addJobLevel">添加</el-button>
    </div>

    <!--表格数据-->
    <div style="margin-top: 10px;">
      <el-table
          @selection-change="handleSelectionChange"
          border
          stripes
          :data="jls"
          size="small"
          style="width: 70%">
        <!--多选框-->
        <el-table-column type="selection" width="55"/>
        <el-table-column
            prop="id"
            label="编号"
            width="55">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="titlelevel"
            label="职称等级"
            width="120">
        </el-table-column>
        <el-table-column
            prop="createdate"
            label="创建日期"
            width="200">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用"
            width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            label="操作">
          <template slot-scope="scope">
            <el-button size="small" @click="showJobLevelEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteHandler(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--如果多选框有数据，就显示批量删除按钮-->
    <el-button type="danger" size="small" style="margin-top: 8px;"
               :disabled="this.multipleSelection==0" @click="deleteMany">批量删除
    </el-button>

    <!--  编辑职称的弹框-->
    <el-dialog
        title="编辑职称"
        :visible.sync="showJobLevelEditFlag"
        width="30%"
        :before-close="handleClose">
      <table>
        <tr>
          <td>
            <el-tag>职称名称</el-tag>
          </td>
          <td>
            <el-input v-model="updateJl.name" size="small" placeholder="请输入职称名称" style="margin-left: 6px;"></el-input>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>职称等级</el-tag>
          </td>
          <td>
            <el-select size="small" v-model="updateJl.titlelevel" placeholder="职称等级"
                       style="margin-left: 6px;margin-right: 6px;">
              <el-option
                  v-for="item in titleLevels"
                  :key="item"
                  :label="item"
                  :value="item">
              </el-option>
            </el-select>
          </td>
        </tr>
        <tr>
          <td>
            <el-tag>是否启用</el-tag>
          </td>
          <td>
            <el-switch
                style="margin-left: 6px;"
                v-model="updateJl.enabled"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-text="已启用"
                inactive-text="未启用">
            </el-switch>
          </td>
        </tr>
      </table>
      <span slot="footer" class="dialog-footer">
    <el-button @click="showJobLevelEditFlag = false">取 消</el-button>
    <el-button type="primary" @click="doUpdate">确 定</el-button>
  </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  name: "JoblevelMana",
  data() {
    return {
      jl: {
        name: '',
        titlelevel: '',
      },
      updateJl: {
        name: '',
        titlelevel: '',
        enabled:false,
      },
      titleLevels: [
        "正高级",
        "副高级",
        "中级",
        "初级",
        "员级"
      ],
      jls: [],
      showJobLevelEditFlag: false,
      multipleSelection:[],
    }
  },
  mounted() {
    this.initJls();
  },
  methods: {
    // 初始化表格数据
    initJls() {
      this.getRequest('/system/basic/joblevel/')
          .then(result => {
            if (result) {
              this.jls = result;
            }
          })
    },
    // 添加职称
    addJobLevel() {
      if (this.jl.name || this.jl.titlelevel) {
        this.postRequest('/system/basic/joblevel/add', this.jl)
            .then(result => {
              if (result) {
                console.log(11)
                console.log(result);
                this.initJls();
                this.jl.name = '';
                this.jl.titlelevel = '';
              }
            })
      } else {
        this.$message.error("请输入职称等级");
      }
    },

    // 更新职称弹框显示
    showJobLevelEdit(data) {
      // 回显数据
      Object.assign(this.updateJl, data);
      this.updateJl.createdate = '';
      this.showJobLevelEditFlag = true;
    },
    handleClose() {
      this.showJobLevelEditFlag = false;
    },
    doUpdate(){
      this.putRequest('/system/basic/joblevel/',this.updateJl)
      .then(result=>{
        if (result){
          this.initJls();
          this.showJobLevelEditFlag=false;
        }
      })
    },

    // 删除一条职称信息
    deleteHandler(data) {
      this.$confirm('此操作将永久删除[' + data.name + ']职称, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/joblevel/' + data.id)
            .then(result => {
              if (result) {
                // 重新加载页面
                this.initJls();
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

    // 批量删除
    handleSelectionChange(val){
      this.multipleSelection = val;
    },
    deleteMany(){
      this.$confirm('此操作将永久删除' + this.multipleSelection.length + '条职位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item => {
          // 拼接成id字符串数组
          ids += 'ids=' + item.id + '&';
        })
        this.deleteRequest('/system/basic/joblevel/' + ids)
            .then(result => {
              if (result) {
                // 重新加载页面
                this.initJls();
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style scoped>

</style>